package com.seeTools.boot.pc.service.sys.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.boot.api.entity.sys.SysDictData;
import com.seeTools.boot.api.entity.sys.SysDictType;
import com.seeTools.boot.common.constant.Constants;
import com.seeTools.boot.common.core.PageQuery;
import com.seeTools.boot.common.tools.StreamUtils;
import com.seeTools.boot.common.tools.redis.RedisUtil;
import com.seeTools.boot.pc.mapper.sys.SysDictTypeMapper;
import com.seeTools.boot.pc.service.BaseServiceImpl;
import com.seeTools.boot.pc.service.sys.SysDictDataService;
import com.seeTools.boot.pc.service.sys.SysDictTypeService;
import com.seeTools.boot.pc.util.PageQueryHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @Author: seeTools
 * @Date: 2024/6/7  16:43
 * @Version 1.0
 */
@Service
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {


    @Resource
    private PageQueryHelper<SysDictType> pageQueryHelper;
    @Resource
    private SysDictDataService sysDictDataService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 分页查询
     *
     * @param pageQuery 分页数据
     * @return 对象
     */
    @Override
    public Page<SysDictType> findList(PageQuery<SysDictType> pageQuery) {
        QueryWrapper<SysDictType> queryWrapper = getQueryWrapper(pageQuery.getDataObj());
        return pageQueryHelper.pageInfo(this, pageQuery, queryWrapper);
    }


    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public List<SysDictType> selectDictTypeList(SysDictType dictType) {
        QueryWrapper<SysDictType> queryWrapper = getQueryWrapper(dictType);
        return list(queryWrapper);
    }


    /**
     * 校验字典类型称是否唯一
     *
     * @param sysDictType 字典类型
     * @return 结果
     */
    @Override
    public boolean checkDictTypeUnique(SysDictType sysDictType) {
        boolean exist = baseMapper.exists(new LambdaQueryWrapper<SysDictType>()
                .eq(SysDictType::getDictType, sysDictType.getDictType())
                .ne(ObjectUtil.isNotNull(sysDictType.getId()), SysDictType::getId, sysDictType.getId()));
        return !exist;
    }


    /**
     * 重置字典缓存数据
     */
    @Override
    public void resetDictCache() {
        clearDictCache();
        loadingDictCache();
    }


    /**
     * 清空字典缓存数据
     */
    @Override
    public void clearDictCache() {
        redisUtil.removeAll(Constants.CacheKey.SYS_DICT);
    }


    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache() {
        List<SysDictData> dictDataList = sysDictDataService.list(new LambdaQueryWrapper<SysDictData>().eq(SysDictData::getStatus, Constants.DictType.DICT_NORMAL));
        Map<String, List<SysDictData>> dictDataMap = StreamUtils.groupByKey(dictDataList, SysDictData::getDictType);
        dictDataMap.forEach((k, v) -> {
            List<SysDictData> dictList = StreamUtils.sorted(v, Comparator.comparing(SysDictData::getDictSort));
            redisUtil.lSet(Constants.CacheKey.SYS_DICT, dictList);
        });
    }

    /**
     * 获取查询条件
     *
     * @param sysDictType
     * @return
     */
    public QueryWrapper<SysDictType> getQueryWrapper(SysDictType sysDictType) {
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysDictType> lambdaQueryWrapper = queryWrapper.lambda();
        // 字典名称
        if (StrUtil.isNotBlank(sysDictType.getDictName())) {
            lambdaQueryWrapper.eq(SysDictType::getDictName, sysDictType.getDictName());
        }
        // 字典状态
        if (StrUtil.isNotBlank(sysDictType.getActive())) {
            lambdaQueryWrapper.eq(SysDictType::getActive, sysDictType.getActive());
        }
        // 字典类型
        if (StrUtil.isNotBlank(sysDictType.getDictType())) {
            lambdaQueryWrapper.like(SysDictType::getDictType, sysDictType.getDictType());
        }
        return queryWrapper;
    }
}
